package com.summer.peony.myinterface.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.summer.peony.myinterface.mapper.MyInterfaceMapper;
import com.summer.peony.myinterface.pojo.MyInterface;
import com.summer.peony.myinterface.pojo.MyInterfaceExample;
import com.summer.peony.myinterface.pojo.MyInterfaceExample.Criteria;
import com.summer.peony.myinterface.service.MyInterfaceService;

@Transactional
@Service
public class MyInterfaceServiceImpl implements MyInterfaceService {

	@Autowired
	private MyInterfaceMapper myInterfaceMapper;

	@Override
	public Integer addInterface(MyInterface myInterface) {
		// 新增接口案例
		Date date = new Date();
		myInterface.setCreateTime(date);
		myInterface.setUpdateTime(date);
		// 案例0：未执行
		myInterface.setResult(0);
		// 是否删除 1：已删除 0：正常
		myInterface.setDelFlag("0");
		return myInterfaceMapper.insert(myInterface);
	}

	@Override
	public Integer updateInerface(MyInterface myInterface) {
		// 修改接口案例
		MyInterface myInterface2 = myInterfaceMapper.selectByPrimaryKey(myInterface.getId());
		Date date = new Date();
		myInterface2.setUpdateTime(date);
		myInterface2.setName(myInterface.getName());
		myInterface2.setRequest(myInterface.getRequest());
		myInterface2.setAddress(myInterface.getAddress());
		myInterface2.setMethod(myInterface.getMethod());
		myInterface2.setPport(myInterface.getPport());
		myInterface2.setCoding(myInterface.getCoding());
		myInterface2.setBody(myInterface.getBody());
		myInterface2.setRemarks(myInterface.getRemarks());
		myInterface2.setExpected(myInterface.getExpected());
		return myInterfaceMapper.updateByPrimaryKey(myInterface2);
	}

	@Override
	public Integer deleteInterface(Integer id) {
		// 删除接口案例
		MyInterface myInterface = myInterfaceMapper.selectByPrimaryKey(id);
		// 是否删除 1：已删除 0：正常
		myInterface.setDelFlag("1");
		return myInterfaceMapper.updateByPrimaryKey(myInterface);
	}

	@Override
	public PageInfo<MyInterface> selectAllInterface(Integer id, String name, Integer result, Integer pageNum,
			Integer pageSize) {
		// 分页查询接口案例列表
		MyInterfaceExample example = new MyInterfaceExample();
		Criteria criteria = example.createCriteria();
		if (id != null && result != null) {
			criteria.andDelFlagEqualTo("0").andIdEqualTo(id).andResultEqualTo(result).andNameLike("%" + name + "%");
		} else if (id == null && result != null) {
			criteria.andDelFlagEqualTo("0").andResultEqualTo(result).andNameLike("%" + name + "%");
		} else if (id != null && result == null) {
			criteria.andDelFlagEqualTo("0").andIdEqualTo(id).andNameLike("%" + name + "%");
		} else {
			criteria.andDelFlagEqualTo("0").andNameLike("%" + name + "%");
		}
//		criteria.andDelFlagEqualTo("0").andIdEqualTo(id).andResultEqualTo(result).andNameLike("%" + name + "%");
		PageHelper.startPage(pageNum, pageSize);
		List<MyInterface> list = myInterfaceMapper.selectByExample(example);
		PageInfo<MyInterface> pageInfo = new PageInfo<MyInterface>(list);
		return pageInfo;
	}

}
